package com.dgut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dgut.dto.ContractSaveDTO;
import com.dgut.entity.Products;
import com.dgut.entity.SaleOrders;
import com.dgut.entity.Salescontracts;
import com.dgut.exception.BaseException;
import com.dgut.mapper.SalesOrdersMapper;
import com.dgut.mapper.SalescontractsMapper;
import com.dgut.service.SalescontractsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhouxiaojun0512
 * @description 针对表【salescontracts】的数据库操作Service实现
 * @createDate 2024-11-24 11:04:09
 */
@Service
@Slf4j
public class SalescontractsServiceImpl extends ServiceImpl<SalescontractsMapper, Salescontracts>
        implements SalescontractsService {
    @Autowired
    private SalesOrdersMapper salesOrdersMapper;

    /**
     * 保存合同和商品信息
     *
     * @param contractSaveDTO
     */
    @Override
    @Transactional
    public void saveWithProduct(ContractSaveDTO contractSaveDTO) {
        // 提取数据
        Salescontracts salescontracts = new Salescontracts();
        BeanUtils.copyProperties(contractSaveDTO, salescontracts);

        // 保存合同
        this.save(salescontracts);

        // 获取合同id
        Long contractId = salescontracts.getContractId();
        // 获取商品列表
        List<Products> productList = contractSaveDTO.getProducts();

        if (productList == null || productList.size() == 0) {
            log.info("没有商品");
            // 抛出异常
            throw new RuntimeException("没有商品");
        } else {
            // 新建订单列表
            List<SaleOrders> saleOrdersList = productsToSaleOrders(productList, contractId);
            // 批量保存
            salesOrdersMapper.insertBatch(saleOrdersList);
        }

    }

    @Override
    @Transactional
    public void updateWithOrder(ContractSaveDTO contractSaveDTO) {
        // 提取数据
        Salescontracts salescontracts = new Salescontracts();
        BeanUtils.copyProperties(contractSaveDTO, salescontracts);
        this.updateById(salescontracts);

        Long contractId = contractSaveDTO.getContractId();
        List<Products> products = contractSaveDTO.getProducts();
        if (products == null || products.size() == 0) {
            log.info("没有修改商品种类");
            List<SaleOrders> saleOrders = contractSaveDTO.getSaleOrders();
            if (saleOrders == null || saleOrders.size() == 0) {
                // 抛出异常
                throw new BaseException("合同没有商品订单");
            } else {
                salesOrdersMapper.delete(new QueryWrapper<SaleOrders>().eq("contract_id", contractId));
                salesOrdersMapper.insertBatch(saleOrders);
            }
        } else {
            List<SaleOrders> saleOrders = productsToSaleOrders(products, contractId);

            // 先删除原来的合同相关订单
            QueryWrapper<SaleOrders> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("contract_id", contractId);
            salesOrdersMapper.delete(queryWrapper);

            // 在提交新的订单
            salesOrdersMapper.insertBatch(saleOrders);
        }
    }

    private List<SaleOrders> productsToSaleOrders(List<Products> products, Long contractId) {
        return products.stream().map(product -> {
            SaleOrders saleOrders = new SaleOrders();
            BeanUtils.copyProperties(product, saleOrders);
            saleOrders.setContractId(contractId);
            return saleOrders;
        }).collect(Collectors.toList());
    }
}




